#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* This program implements a stack in C using an array  */


#define SIZE 1000 
void main() {
   int stack[SIZE]; // The stack array
   int top ; // The top of the stack
   int kind ; // The kind of operation  0=PUSH, 1=POP, 2=PRINT
   int data ; // The data stored in the stack
   int i ;
  


   // Initializations 
   top = 0 ;

   // Main loop 
   while (scanf("%d", &kind) != EOF) {
	   switch(kind) {
	   case 0: // push
		   if ( top >= SIZE ) {
			   fprintf(stderr, "fatal error: stack overflow\n");
			   exit(1);
		   }
		   fprintf(stderr, "enter data to push\n");
		   scanf("%d", &data);
		   stack[top] = data;
		   top = top + 1 ;
		   break;
	   case 1: // pop
		   if (  top <= 0) {
			   fprintf(stderr, "fatal error: stack underflow\n");
			   exit(2);
		   }
		   top = top - 1 ;
		   printf("top of the stack is %d\n", stack[top]) ;
		   break;
	   case 2: // print
		   i = 0 ;
		   while (i < top ) {
			   printf("stack[%d]=%d\n", i, stack[i]);
			   i = i + 1 ;
		   }
		   break;

	   default: 
		   fprintf(stderr, "fatal error: unknown operation\n");		   
		   exit(3);
		   break ;
	   }
   }
}
		

